package net.crowdloud.client.problem;

import net.crowdloud.client.user.ButtonUserPage;
import net.crowdloud.shared.entities.Problem;
import net.crowdloud.shared.entities.User;
import net.crowdloud.shared.utilities.FormField;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 
 * @UserManual
 */
public class ProblemForm extends VerticalPanel {

	public static final TextArea description = new TextArea();

	public ProblemForm(User user, Problem problem) {

		this.setBorderWidth(1);

		VerticalPanel vp = new VerticalPanel();

		vp.setSpacing(30);

		vp.add(new ButtonUserPage(user));

		description.setSize("500px", "100px");
		description.setValue(problem.getDescription());
		vp.add(new FormField("Problem Description", description));

		vp.add(new Label("TAGS: " + problem.getTags()));

		vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		HorizontalPanel hp = new HorizontalPanel();

		hp.setWidth("300px");

		hp.add(buttonSave(user, problem));

		vp.add(hp);

		this.add(vp);
	}

	private Button buttonSave(final User user, final Problem problem) {

		Button button = new Button("Save", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				problem.setDescription(description.getValue());

				new UpdateProblem(user, problem);
			}
		});

		return button;
	}

}
